/**
* 头部菜单
*/
<template>
  <div style="background-color: #ffffff">
    <el-button class="buttonimg">
      <img alt="加载失败" class="showimg" :src="collapsed?imgsq:imgshow" @click="toggle(collapsed)">
      <!--面包屑-->
      <el-breadcrumb separator-class="el-icon-arrow-right" class="crumbs">
        <el-breadcrumb-item to="/home">
          首页
        </el-breadcrumb-item>
        <el-breadcrumb-item v-for="item in currentMenu" :key="currentMenu.id" v-if="currentMenu">
          {{ item.name }}
        </el-breadcrumb-item>
      </el-breadcrumb>
    </el-button>


    <el-dropdown style="float: right">
      <div style="float: right">
        <!--        <img :src="this.ossaddr+'/'+u.avatar" alt>-->
        <el-avatar shape="square" :size="50" :src="url"></el-avatar>
      </div>
      <el-dropdown-menu slot="dropdown">
        <router-link to="/myCenter" style="text-decoration: none">
          <el-dropdown-item>
            个人中心
          </el-dropdown-item>
        </router-link>
        <el-dropdown-item @click.native="exit">
          退出
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
    <div style="float: right">
      <el-tag>
        角色：【<label v-for="item in role" style="display: initial;margin: 5px">{{ item.name }}</label>】
      </el-tag>
    </div>
  </div>
</template>
<script>
import {mapState} from 'vuex'
import loginAPI from "../api/basicfunction/basiclogin/loginAPI";
import myCenterAPI from "../api/basicfunction/basicmenu/myCenterAPI";

export default {
  name: 'headtop',
  computed: {
    ...mapState(['currentMenu']),
    ...mapState(['ossaddr'])
  },
  data() {
    return {
      collapsed: true,
      imgshow: require('../assets/img/right.gif'),
      imgsq: require('../assets/img/left.gif'),
      url: null,
      role: []
    }
  },
  methods: {
    // 用户
    getUser() {
      myCenterAPI.getUser().then(({data}) => {
        this.url = this.ossaddr + '/' + data.data.avatar
      })
    },
    // 角色
    getRole() {
      loginAPI.getRoles().then(({data}) => {
        this.role = data.data;
      })
    },
    // 退出登录
    exit() {
      this.$confirm('退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.commit("startAnimation", true);
        setTimeout(() => {
          this.$store.commit("startAnimation", false);
          this.$store.commit('logout', null)
          this.$router.push('/login')
          this.$message({
            type: 'success',
            message: '已退出登录!'
          })
        }, 1000)
      }).catch(() => {
        this.exitloading = false
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    // 切换显示
    toggle(showtype) {
      this.collapsed = !showtype
      this.$root.Bus.$emit('toggle', this.collapsed)
    },
    // 获取绝色
  },
  created() {
    this.getRole();
    this.getUser();
    // 监听修改头像的信息
    this.$root.Bus.$on('userUpdate', value => {
      this.getRole();
      this.getUser();
    })
  }
}
</script>
<style scoped>
.crumbs {
  position: absolute;
  margin-left: 70px;
  margin-top: -25px;
  font-size: 18px;
}

.buttonimg {
  height: 60px;
  background-color: transparent;
  border: none;
}

.showimg {
  height: 30px;
}

.showimg:active {
  border: none;
}

.el-tag {
  margin: 14px 10px;
}

.el-avatar {
  margin: 5px 10px;
}
</style>
